import express = require("express");
import {Request, Response} from "express"
import { Application } from "pinus";
import {Code} from "../../../consts/code";
import * as crypto from "crypto";
import robot from "../../../games/aviator/robot";
import {aviator} from "../../../util/routeUtil";



module.exports = function(app: Application, http: express.Application) {
    const key = app.get('http').key

    http.post('/robot/config', async (request: Request, response: Response) => {
        let sign = request.body.sign
        let timestamp = request.body.timestamp
        if (!sign || !timestamp) {
            return response.end(JSON.stringify({code: Code.ERROR_PARAM}))
        }
        // 验签
        let signStr = 'timestamp=' + timestamp + '&key=' + key
        if (sign !== crypto.createHash('md5').update(signStr).digest('hex')) {
            return response.end(JSON.stringify({code: Code.INVALID_TOKEN}))
        }
        // 拉取最新配置
        if (!robot(app).resetRobot()){
            console.log('重置配置失败');
            return response.end(JSON.stringify({code: Code.ERROR_STATUS}))
        }
        return response.end(JSON.stringify({code: Code.OK}))
    })

    http.post('/game/config', async (request: Request, response: Response) => {
        let sign = request.body.sign
        let timestamp = request.body.timestamp
        if (!sign || !timestamp) {
            return response.end(JSON.stringify({code: Code.ERROR_PARAM}))
        }
        // 验签
        let signStr = 'timestamp=' + timestamp + '&key=' + key
        if (sign !== crypto.createHash('md5').update(signStr).digest('hex')) {
            return response.end(JSON.stringify({code: Code.INVALID_TOKEN}))
        }
        // 拉取最新配置
        if (!robot(app).resetGame()){
            console.log('重置配置失败');
            return response.end(JSON.stringify({code: Code.ERROR_STATUS}))
        }
        const aviators = app.getServersByType("aviator")
        await app.rpc.aviator.remote.reload.toServer(aviators[0].id)
        await app.rpc.aviator.remote.sendToClient.toServer(aviators[0].id)
        return response.end(JSON.stringify({code: Code.OK}))
    })

    http.post('/game/controlSetting', async (request: Request, response: Response) => {
        let sign = request.body.sign
        let timestamp = request.body.timestamp
        if (!sign || !timestamp) {
            return response.end(JSON.stringify({code: Code.ERROR_PARAM}))
        }
        // 验签
        let signStr = 'timestamp=' + timestamp + '&key=' + key
        if (sign !== crypto.createHash('md5').update(signStr).digest('hex')) {
            return response.end(JSON.stringify({code: Code.INVALID_TOKEN}))
        }
        const aviators = app.getServersByType("aviator")
        await app.rpc.aviator.remote.reloadControlSetting.toServer(aviators[0].id)
        return response.end(JSON.stringify({code: Code.OK}))
    })


    http.post('/control/config', async (request: Request, response: Response) => {
        console.log("加载控制配置")
        let sign = request.body.sign
        let timestamp = request.body.timestamp
        if (!sign || !timestamp) {
            return response.end(JSON.stringify({code: Code.ERROR_PARAM}))
        }
        // 验签
        let signStr = 'timestamp=' + timestamp + '&key=' + key
        if (sign !== crypto.createHash('md5').update(signStr).digest('hex')) {
            return response.end(JSON.stringify({code: Code.INVALID_TOKEN}))
        }
        const aviatorServers = app.getServersByType('aviator')
        // 加载控制配置
        await app.rpc.aviator.remote.reloadControl.toServer(aviatorServers[0].id)
        console.log("加载控制配置完成")
        return response.end(JSON.stringify({code: Code.OK}))
    })

}
